package com.chagee.channel.server.controller;


import com.chagee.base.utils.HttpResponse;
import com.chagee.base.utils.HttpResponseUtil;
import com.chagee.channel.api.bo.oss.FileNameListReq;
import com.chagee.channel.api.bo.oss.NewFile;
import com.chagee.channel.api.bo.oss.SignedURLRep;
import com.chagee.channel.api.bo.oss.SignedURLReq;
import com.chagee.channel.common.exception.BizErrorCode;
import com.chagee.channel.server.controller.req.AddWaterMarkReq;
import com.chagee.channel.server.controller.req.ZipOssReq;
import com.chagee.channel.server.event.dto.OssCallbackResult;
import com.chagee.channel.server.event.dto.OssPolicyResult;
import com.chagee.channel.server.event.model.OssPolicyParam;
import com.chagee.channel.server.event.service.OssService;
import com.chagee.channel.server.service.biz.VideoBizService;
import com.chagee.channel.server.service.third.AliyunFunctionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * Oss相关操作接口
 * Created by macro on 2018/4/26.
 */
@RestController
@Api(tags = "OssController", description = "Oss管理")
@RequestMapping("/aliyun/oss")
public class OssController {

    @Resource
    private VideoBizService videoBizService;
    @Resource
    private OssService ossService;

    @Resource
    private AliyunFunctionService aliyunFunctionService;

    @Value("${aliyun.oss.fileUrl:https://chagee20.oss-cn-shanghai.aliyuncs.com/}")
    private String ALIYUN_OSS_FILE_URL;

    @ApiOperation(value = "oss上传签名生成")
    @RequestMapping(value = "/policy", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<OssPolicyResult> policy(OssPolicyParam param) {
        OssPolicyResult result = ossService.policy(param);
        return HttpResponseUtil.success(result);
    }

    @ApiOperation(value = "oss上传成功回调")
    @RequestMapping(value = "callback", method = RequestMethod.POST)
    @ResponseBody
    public HttpResponse<OssCallbackResult> callback(HttpServletRequest request) {
        OssCallbackResult ossCallbackResult = ossService.callback(request);
        return HttpResponseUtil.success(ossCallbackResult);
    }

    @ApiOperation(value = "oss获取签名URL")
    @RequestMapping(value = "/signedURL", method = RequestMethod.POST)
    @ResponseBody
    public HttpResponse<SignedURLRep> signedURL(@RequestBody SignedURLReq param) {
        SignedURLRep result = ossService.signedURL(param);
        return HttpResponseUtil.success(result);
    }

    @ApiOperation(value = "oss获取取视频第一帧")
    @RequestMapping(value = "/firstFrameOfVideo", method = RequestMethod.POST)
    @ResponseBody
    public HttpResponse<SignedURLRep> firstFrameOfVideo(@RequestBody SignedURLReq param) {
        SignedURLRep result = ossService.firstFrameOfVideo(param);
        return HttpResponseUtil.success(result);
    }

    @ApiOperation(value = "oss删除文件")
    @RequestMapping(value = "/deleteFile", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<Void> deleteFile(OssPolicyParam param) {
        if (StringUtils.isEmpty(param.getStorageDirectory())){
            return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), "删除文件不能为空");
        }
        if(!param.getStorageDirectory().contains(ALIYUN_OSS_FILE_URL)){
            return HttpResponseUtil.build(null, BizErrorCode.REQUEST_VALID_FAILED.getErrorCode(), "删除文件地址格式不正确");
        }else{
            param.setStorageDirectory(param.getStorageDirectory().replace(ALIYUN_OSS_FILE_URL,""));
        }
        ossService.deleteFile(param);
        return HttpResponseUtil.success(null);
    }

    @ApiOperation(value = "ossMOV转MP4")
    @RequestMapping(value = "/toMP4File", method = RequestMethod.POST)
    @ResponseBody
    public HttpResponse<Integer> toMP4File(@RequestBody FileNameListReq fileNamelist){
        int result = ossService.toMP4(fileNamelist);
        return HttpResponseUtil.success(result);
    }
    @ApiOperation(value = "oss缓存获取转码后文件")
    @RequestMapping(value = "/getMP4File", method = RequestMethod.GET)
    @ResponseBody
    public HttpResponse<String> getMP4File(String objectName){
        String url = ossService.getMP4(objectName);
        return HttpResponseUtil.success(url);
    }

    @ApiOperation(value = "视频加水印")
    @RequestMapping(value = "/addWaterMark", method = RequestMethod.POST)
    public HttpResponse<Boolean> addWaterMark(@RequestBody AddWaterMarkReq req){
        return HttpResponseUtil.success(videoBizService.addTextWaterMark(req.getVideoUrls(), req.getWaterMarkDto()));
    }

    @ApiOperation(value = "获取带水印的视频")
    @RequestMapping(value = "/getVideoWithWaterMark", method = RequestMethod.POST)
    public HttpResponse<Map<String, String>> getVideoWithWaterMark(@RequestBody List<String> ossUrls){
        return HttpResponseUtil.success(videoBizService.getVideoWithWaterMark(ossUrls));
    }

    @ApiOperation(value = "oss文件打包获取zip地址")
    @RequestMapping(value = "/getZipOssUrl", method = RequestMethod.POST)
    public HttpResponse<String> getZipOssUrl(@RequestBody @NotNull ZipOssReq req){
        return HttpResponseUtil.success(aliyunFunctionService.getZipOssUrl(req));
    }

}
